<?php
/**
 * 获取指定店铺的订单
 *
 * @author hzjsq@msn.com
 * @version 01.
 */
set_time_limit(5);
$argv[1] = '';
$argv[2] = '2012-7-30 00:00:00';
$argv[3] = '2012-7-30 23:59:59';

$domain = $argv[1];
$sDate = strtotime($argv[2]);
$eDate = strtotime($argv[3]);


define('COMMAND_MODE', true);

set_time_limit(0);

define('LIB_DIR', dirname(__FILE__) . '/lib/');


require_once(LIB_DIR . 'api/qinqinbabycom/order.php');
require_once(dirname(__FILE__) . '/lib/init.php');

$orderObj = new tools_api_qinqinbabycom_order();//taobao订单
$dbo = kernel::database();

$tid=$_GET['bn'];

if($tid){
	$orderOme = $orderObj->getFullTrade($tid);
	//var_dump($orderOme);
	if (!empty($orderOme)) {
		//var_dump($orderOme);
		if(insertIntoOrder($orderOme, "1112212")){
			echo "{$tid} done.\n";
		}else
		{
			echo "{$tid} fail.\n";
		}
	}
}else{
	$orders = $orderObj->fetchOrders($sDate, $eDate);
	//var_dump($orders);
	echo "There are  {" .count($orders)."} orders\n";
	foreach ( $orders as $tid ) {
		
		$tsql = "select order_bn from sdb_ome_orders where status='dead'  and order_bn='$tid'";
		$orderbn = $dbo->selectrow ( $tsql );
		if (! $orderbn) {
			$orderOme = $orderObj->getFullTrade ( $tid );

			if (! empty ( $orderOme )) {

				if (insertIntoOrder ( $orderOme, "1112212" )) {
					echo "{$tid} done.\n";
				} else {
					echo "{$tid} fail.\n";
				}
			}
		} else {
			echo "{$tid} has dead.\n";
		}
	}
}


/**
 * 插入订单数据
 * @param array $sdf 订单数据
 * @param array $nodeId 商铺节点
 */
function insertIntoOrder($sdf, $nodeId) {

	static $orderObj = null;
	static $response = null;

	if (!$orderObj) {
		$orderObj = new ome_rpc_response_order();
		$response = kernel::single('base_rpc_service');
	}

	base_rpc_service::$node_id = $nodeId;

	return $orderObj->add($sdf, $response);
}

function getShopList() {

	$db = kernel::database();
	$list = $db->select("SELECT * FROM sdb_ome_shop WHERE disabled='false'");
	if ($list) {
		foreach($list as $key => $item) {

			$list[$key]['addon'] = unserialize($item['addon']);
			$list[$key]['config'] = unserialize($item['config']);
		}
		return $list;
	} else {

		return array();
	}
}